#include <pthread.h>
#include <ctime>
#include <unistd.h>
#include <sys/types.h>
#include <cstdlib>
#include <cstring>
#include "QueueBlock.hpp"
#include "Task.hpp"
//productor todo
void* Productor(void* args)
{
    const char str[] = "+-*/%!#";
    QueueBlock<Task<int>>* que = static_cast<QueueBlock<Task<int>>*>(args);
    while(true)
    {
        int data1 = rand() % 10;
        int data2 = rand() % 10;
        char ch = str[rand() % strlen(str)];
        Task<int> t{data1, data2, ch};
        que->Push(t);
        std::cout << "I send a task : " 
        << data1 << ch << data2 
        << "=?" << std::endl;   
        usleep(100);
    }
}

//consumer todo
void* Consumer(void* args)
{
    QueueBlock<Task<int>>* que = static_cast<QueueBlock<Task<int>>*>(args); 
    while(true)
    {
        Task<int> t;
        que->Pop(&t);
        t.Run();
        std::cout << "I get a task : "
        << t._data1 << t._rule << t._data2 << "=" << t._result
        << "[" << t._rerrno << "]" << std::endl;
    }
}

int main()
{
    srand(time(nullptr) ^ getpid() ^ pthread_self() ^ getppid());
    //定义阻塞队列
    QueueBlock<Task<int>>* que = new QueueBlock<Task<int>>;

    //创建生产者thread
    pthread_t productor;
    pthread_create(&productor, nullptr, Productor, que);

    //创建消费者thread
    pthread_t consumer;
    pthread_create(&consumer, nullptr, Consumer, que);

    //等待生产者、消费者
    pthread_join(productor, nullptr);
    pthread_join(consumer, nullptr);
    return 0;
}